package com.huanwei.login_regist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.huanwei.mini.R;
import com.huanwei.mini.http.API;
import com.huanwei.mini.http.Login;
import com.huanwei.mini.utils.JSONUtils;
import com.huanwei.mini.utils.Okhttp;
import com.huanwei.mini.utils.SpfUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录页面，
 */
public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_mm)
    EditText loginMm;
    @BindView(R.id.but_login)
    Button butLogin;
    @BindView(R.id.but_regist)
    Button butRegist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.img_back, R.id.but_login, R.id.but_regist   })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;


            case R.id.but_login:
                if (TextUtils.isEmpty(loginPhone.getText().toString())) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(loginMm.getText().toString())) {
                    Toast.makeText(this, "请密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                API.login(loginPhone.getText().toString(), loginMm.getText().toString(), new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("1111", "数据："+st);
                        Login login = JSONUtils.parseJSON(st, Login.class);
                        if (login.getError()==0) {
                            Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            SpfUtils.put(LoginActivity.this, "islogin", true);
                            SpfUtils.put(LoginActivity.this, "sessionkey", login.getSessionkey());
                            SpfUtils.put(LoginActivity.this, "sessionuid",""+ login.getSessionuid());
                            SpfUtils.put(LoginActivity.this, "sessionxid", ""+login.getSessionxid());
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, "登录失敗，"+login.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                        //                 if (!z_userInfoDao.getpassword(loginPhone.getText().toString()).equals(loginMm.getText().toString())) {
//                        Toast.makeText(this, "密码错误！", Toast.LENGTH_SHORT).show();
//                    } else {
//                        Toast.makeText(this, "登录成功！", Toast.LENGTH_SHORT).show();
//                        startActivity(new Intent(this, MainActivity.class));
//                        SpfUtils.put(this, "islogin", true);
//                        SpfUtils.put(this, "type", "2");
//                        SpfUtils.put(this, "name", z_userInfoDao.getname(loginPhone.getText().toString()));
//
//                    }
                    }

                    @Override
                    public void onFalia(int code, String errst) {

                    }
                });



                break;
            case R.id.but_regist:
                startActivity(new Intent(this, RegistActivity.class));
                break;
        }
    }
}
